package gfx.tiles;

import java.awt.Graphics;

import core.Main;
import core.Vector2;

public class TileRenderer {

    private static int[] tiles;  		//colors of the tiles, given from the map image.
	
	/**
     * Renders all visible tiles in the window.
     * 
     * @param g Graphics object used to render all tiles on.
     */
    public static void renderTiles(Graphics g, Vector2 renderPos, int frameWidth, int frameHeight) {
    	Tile.size = (int)(Tile.START_SIZE * Main.zoom);
    	
    	renderPos.multiply(Main.zoom);
    	
        int x0 = (int) (renderPos.x - Tile.size) / Tile.size;
        int x1 = (int) (renderPos.x + frameWidth + Tile.size) / Tile.size;
        int y0 = (int) (renderPos.y - Tile.size) / Tile.size;
        int y1 = (int) (renderPos.y + frameHeight + Tile.size) / Tile.size;

        for (int y = y0; y < y1; y++) {
            for (int x = x0; x < x1; x++) {
                renderTile(renderPos, x * Tile.size, y * Tile.size, getTile(x, y), g);
            }
        }
    }
    
    /**
     * Renders a single tile at a given position. xp and yp in world coordinates.
     * 
     * @param xp World position of the tile.
     * @param yp World position of the tile.
     * @param tile Tile to render.
     * @param g Graphics object used to render the tile.
     */
    private static void renderTile(Vector2 renderPos, int xp, int yp, Tile tile, Graphics g) {
        xp -= (int) renderPos.x;
        yp -= (int) renderPos.y;
        g.drawImage(tile.getTileImage(), xp, yp, Tile.size, Tile.size, null);
    }
    
    /**
     * Calculates whether an object at a given position and speed will collide with a solid tile.
     * 
     * @param pos The position of something in a Vector2.
     * 
     * @return Returns true if a solid tile is at the calculated position.
     */
    public static int tileCollision(Vector2 pos, Vector2 size) {
    	int tileX;
    	int tileY;
    	pos = new Vector2(pos.x - size.x / 2, pos.y - size.y / 2);
    	
    	for (int c = 0; c < 4; c++) {
            tileX = (((int)(pos.x + c % 2 * size.x))) / Tile.size;
            tileY = (((int)(pos.y + c / 2 * size.y))) / Tile.size;
            int tile = getTile(tileX, tileY).state();
            if (tile != Tile.AIR) return tile;
        }
    	
    	return Tile.AIR;
    }
    
    /**
     * Returns a tile at a given position. x and y in tile coordinates.
     * 
     * @param x X position in tile coordinates.
     * @param y Y Position in tile coordinates.
     * 
     * @return Returns a tile at the given position if existing and in the world.
     */
    public static Tile getTile(int x, int y) {
        if (x < 0 || y < 0 || x >= Main.worldWidth || y >= Main.worldHeight) return Tile.err;
        
//        if (tiles[y * worldWidth + x] == Tile.tank.getColor()) return Tile.tank;
        
        for (int i = 0; i < Tile.tiles.size(); i++) {
            if (tiles[y * Main.worldWidth + x] == Tile.tiles.get(i).getColor()) return Tile.tiles.get(i);
        }
        
        return Tile.err;
    }
    
    public static void addTileColors(int[] tiles) {
    	TileRenderer.tiles = tiles;
    }
	
}
